package com.example.demo_01.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/codeController")
public class CodeController {

    //验证码
    @GetMapping("/code")
    public void code(HttpSession session, HttpServletResponse response) throws IOException, IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha =
                CaptchaUtil.createLineCaptcha(120, 38, 4, 10);
        //获取验证码的文本存放在Session中
        session.setAttribute("code", lineCaptcha.getCode());
        //将验证码图片发给浏览器
        lineCaptcha.write(response.getOutputStream());
    }
}
